from ST7735 import TFT
from machine import SPI,Pin
import time
import network
#hardware SPI, HSPI
spi=SPI(1,baudrate=40000000, polarity=0, phase=0, sck=Pin(2), mosi=Pin(3))
tft=TFT(spi,'/combined.bin',6,10,7)# dc, rst, cs   #6,10,7 aDC, aReset, aCS
tft.rgb(False)
tft.init_7735(tft.REDTAB80x160)
counts = 1

# network connect
wlan = network.WLAN(network.STA_IF)
wlan.active(True)

while True:
    
    if wlan.isconnected():
        ip_address = wlan.ifconfig()[0]
        pass
    else:
        try:
            wlan.connect('name', 'passwd')
            ip_address = wlan.ifconfig()[0]
            print(ip_address)
        except:
            print('failed to connect')
            ip_address = "No_Internet"
        
    tft.fill(TFT.WHITE)
    chars = 'IP:' + str(ip_address)
    tft.text((0,0),chars,TFT.BLACK,(2,2))
    time.sleep(2)
    
    tft.fill(TFT.WHITE)
    chars = '卑鄙是卑鄙者的通行证.....高尚是高尚者的墓志铭....'
    tft.text((0,0),chars,TFT.BLACK,(1,1))
    time.sleep(2) 
         
    tft.fill(TFT.WHITE)
    chars = f"本次功德------" + str(counts)
    counts += 1
    tft.text((0,0),chars,TFT.CYAN,(2,2))
    time.sleep(2)
    
    mytime = time.localtime()

    # 获取小时、分钟和秒，并添加零前缀
    hours = mytime[3] % 24
    minutes = mytime[4]
    seconds = mytime[5]

    hours_str = f'{hours:02d}'
    minutes_str = f'{minutes:02d}'
    seconds_str = f'{seconds:02d}'

    Ltime = f'{mytime[0]}-{mytime[1]}-{mytime[2]}----{hours_str}:{minutes_str}'
    time.sleep(1)
    
    tft.fill(TFT.WHITE)
    chars = str(Ltime)
    tft.text((0,0),chars,TFT.RED,(2,2))
    time.sleep(2)
    
